package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.redis.service.IEmployeeRedisService;
import cn.wolfcode.service.IEmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private IEmployeeRedisService employeeRedisService;
    @Override
    public Employee login(String name, String password) {
        //判断员工是否存在
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq(name != null,"name",name);
        wrapper.eq(password != null,"password",password);
        Employee employee = getOne(wrapper);
        if (employee == null){
            throw new RuntimeException("账号或者密码错误");
        }
        //如果存在则把用户权限存在redis中
        employeeRedisService.saveEmployeePermission(employee);
        return employee;
    }
}
